package com.kyzh.core.activities

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Collection
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.adapters.CollectionAdapter

import com.scwang.smart.refresh.layout.SmartRefreshLayout
import kotlinx.android.synthetic.main.recyclerview.*
import kotlinx.android.synthetic.main.titlebar.*

class DealCollectionActivity : BaseActivity() {

    private var p1 = 1
    private var max1 = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deal_collection)
        initView()
    }

    private fun initView() {
        close.setOnClickListener { finish() }
        tvTitle.text = getString(R.string.myCollect)
        val beans = arrayListOf<Collection>()
        recyclerView.layoutManager = object : LinearLayoutManager(this@DealCollectionActivity) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        p1 = 1
        max1 = 1
        val adapter = CollectionAdapter(beans)
        recyclerView.adapter = adapter
        root.autoRefresh()
        root.setEnableAutoLoadMore(false)
        root.setOnRefreshListener {
            p1 = 1
            addData(root, beans, adapter)
        }

        root.setOnLoadMoreListener {
            if (p1 > max1) {
                getString(R.string.noHaveMore).toast()
                root.finishLoadMore()
            } else {
                addData(root, beans, adapter)
            }
        }
    }

    private fun addData(root: SmartRefreshLayout, bean: ArrayList<Collection>, adapter: CollectionAdapter) {
        UserImpl.myCollect(2, p1, object : ResultListener {
            override fun error() {
                adapter.setEmptyView(R.layout.empty)
                root.finishRefresh()
                root.finishLoadMore()
            }

            override fun success(beans: Any, p: Int, max: Int) {
                beans as ArrayList<Collection>
                root.finishRefresh()
                root.finishLoadMore()
                if (p == 2) {

                    bean.clear()
                }
                bean.addAll(beans)
                adapter.notifyDataSetChanged()
                p1 = p
                max1 = max
            }
        })
    }
}
